package com.df.bpm.utils.businessViewerV2.informatization;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.human.C_human;
import com.df.bpm.utils.businessViewerV2.human.M_human;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.List;

public class C_requirement {

    static Db_requirement database=new Db_requirement();

    static int editRequirement(JTable iTable, int row, List<M_requirementBasicDto> list, int editedCol) {
        String oldValue=""+iTable.getValueAt(row, editedCol);
        int rows=0;
        String tips="请输入新内容：";
        Object newValue=JOptionPane.showInputDialog(null, tips, "新内容", JOptionPane.PLAIN_MESSAGE, null, null, oldValue);
        if(newValue!=null && !oldValue.equals(newValue)){
            iTable.setValueAt(newValue, row, editedCol);
            M_requirement temp = list.get(row);
            if(editedCol==2){
                temp.setState(Integer.parseInt(""+newValue) );
            }else if(editedCol==3){
                temp.setNumber((String) newValue);
            }else if(editedCol==4){
                temp.setName((String) newValue);
            }else if(editedCol==6){
                temp.setTips((String) newValue);
            }

            if(newValue.equals("-")){
                if(CommonUtils.varIsNotBlank(temp.getInformatizationRequirementIndex())){
                    M_requirementDetailsDto tempDto=new M_requirementDetailsDto();
                    tempDto.setBasic(temp);
                    int ir=database.deleteDetailsDto(tempDto);
                    rows+=ir;
                    if(ir>0){
                        list.remove(row);
                        DefaultTableModel model=(DefaultTableModel)(iTable.getModel());
                        model.removeRow(row);
                    }
                }
            }else {
                if(temp.getInformatizationRequirementIndex()==null){
                    if(temp.getNumber()!=null && temp.getName()!=null){
                        database.newDetailsDto(new M_requirementDetailsDto().setBasic(temp));
                        rows+=1;
                    }
                }else {
                    rows+=database.update(new M_requirementDetailsDto().setBasic(temp));
                }
            }
        }
        return rows;
    }

    public static void changeDeveloper(JTable iTable, int row, List<M_requirementBasicDto> list) throws Exception {
        M_human developer= C_human.selectOne();
        if(CommonUtils.varIsNotBlank(developer) && !developer.getEmployeeListIndex().equals(list.get(row).getEmployeeListIndex())){
            iTable.setValueAt(developer.getNumber(), row, 7);
            iTable.setValueAt(developer.getName(), row, 8);
            M_requirementBasicDto temp = list.get(row);
            temp.setEmployeeListIndex(developer.getEmployeeListIndex());
            database.update(new M_requirementDetailsDto().setBasic(temp));
        }
    }

    public static List<M_requirementBasicDto> search(List<JTextField> fields) {
        M_requirementBasicDto temp=new M_requirementBasicDto();
        if (CommonUtils.varIsNotBlank(fields.get(0).getText())) {
            temp.setNumber(fields.get(0).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(1).getText())) {
            temp.setName(fields.get(1).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(2).getText())) {
            temp.setTips(fields.get(2).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(3).getText())) {
            temp.setDeveloperNumber(fields.get(3).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(4).getText())) {
            temp.setDeveloperName(fields.get(4).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(5).getText())) {
            temp.setState(Integer.valueOf(fields.get(5).getText()));
        }
        return database.getList4Like(temp);
    }

    public static M_requirementDetailsDto getDetails(Integer requirementID) {
        return database.getDetailsDto(requirementID);
    }
    public static void show() {
        V_requirement vRequirement=new V_requirement();
        vRequirement.repaint();
        vRequirement.getFields().get(5).setText("1");
        vRequirement.setVisible(true);
    }

    public static void show(JFrame jf) {
        V_requirement vRequirement=new V_requirement();
        vRequirement.repaint(jf);
        vRequirement.getFields().get(5).setText("1");
    }

    public static void main(String[] args) throws Exception {
        Main.getScreenMaxSize();
        show();
    }

    public static void saveDetails(M_requirementDetailsDto detailsDto) {
        database.saveDetails(detailsDto);
    }

    public static void deleteReason(M_reason mReason) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_reason> temp_reason=new ArrayList<>();
        temp_reason.add(mReason);
        temp.setReasons(temp_reason);
        database.deleteDetailsDto(temp);
    }

    public static void updateReason(M_reason mReason) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_reason> temp_reason=new ArrayList<>();
        temp_reason.add(mReason);
        temp.setReasons(temp_reason);
        database.update(temp);
    }

    public static M_reason addReason(M_requirement requirementBasic, M_reason mReason) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_reason> temp_reason=new ArrayList<>();
        temp_reason.add(mReason);
        temp.setReasons(temp_reason);
        temp.setBasic(requirementBasic);
        temp=database.newDetailsDto(temp);
        return temp.getReasons().get(0);
    }

    public static void deleteTarget(M_target mTarget) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_target> temp_target=new ArrayList<>();
        temp_target.add(mTarget);
        temp.setTargets(temp_target);
        database.deleteDetailsDto(temp);
    }

    public static void updateTarget(M_target mTarget) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_target> temp_target=new ArrayList<>();
        temp_target.add(mTarget);
        temp.setTargets(temp_target);
        database.update(temp);
    }

    public static M_target addTarget(M_requirement requirementBasic, M_target mTarget) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_target> temp_target=new ArrayList<>();
        temp_target.add(mTarget);
        temp.setTargets(temp_target);
        temp.setBasic(requirementBasic);
        temp=database.newDetailsDto(temp);
        return temp.getTargets().get(0);
    }

    public static void deleteOutput(M_outputDTO mOutput) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_outputDTO> outputs=new ArrayList<>();
        outputs.add(mOutput);
        temp.setOutputs(outputs);
        database.deleteDetailsDto(temp);
    }

    public static void updateOutput(M_outputDTO mOutput) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_outputDTO> outputs=new ArrayList<>();
        outputs.add(mOutput);
        temp.setOutputs(outputs);
        database.update(temp);
    }

    public static M_outputDTO addOutput(M_requirement requirementBasic, M_outputDTO mOutput) {
        M_requirementDetailsDto temp=new M_requirementDetailsDto();
        List<M_outputDTO> outputs=new ArrayList<>();
        outputs.add(mOutput);
        temp.setOutputs(outputs);
        temp.setBasic(requirementBasic);
        temp=database.newDetailsDto(temp);
        return temp.getOutputs().get(0);
    }

    public static void updateDetails(M_requirementDetailsDto temp) {
        database.update(temp);
    }
}
